<template>
  <div class="app-container">
    <div>
      <h1>教师课程列表</h1>
    </div>
    <el-button @click="addClass()"> 添加班级 </el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Name" width="150">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="Introduction" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.introduction }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="450">
        <template slot-scope="scope">
          <el-button @click="enterClass(scope.row.id)"> 学生管理 </el-button>
          <el-button @click="testManage(scope.row.id)"> 考试管理 </el-button>
          <el-button @click="changeClass(scope.$index)"> 修改信息 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="changeShow">
      <el-form :model="classDetail">
        <el-form-item label="班级id">
          <el-input v-model="classDetail.c_id" :disabled="true" />
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="classDetail.new_name" />
        </el-form-item>
        <el-form-item label="班级简介">
          <el-input v-model="classDetail.new_introduction" />
        </el-form-item>
        <el-button type="primary" style="width:100%;margin-bottom:30px;" @click="handleChange">提交</el-button>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="addShow">
      <el-form :model="newClass">
        <el-form-item label="班级名称">
          <el-input v-model="newClass.c_name" placeholder="请输入班级名称" />
        </el-form-item>
        <el-form-item label="班级简介">
          <el-input v-model="newClass.c_introduction" placeholder="请输入班级简介" />
        </el-form-item>
        <el-button type="primary" style="width:100%;margin-bottom:30px;" @click="handleAdd">提交</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getTeacherClasses } from '@/api/classes'
import { teacherChangeClass } from '@/api/classes'
import { teacherAddClass } from '@/api/classes'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      classDetail: {
        c_id: '',
        new_name: '',
        new_introduction: ''
      },
      newClass: {
        t_id: this.$store.getters.id,
        c_name: '',
        c_introduction: ''
      },
      changeShow: false,
      addShow: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getTeacherClasses({ tid: this.$store.getters.id }).then(response => {
        this.list = response.classes
        this.listLoading = false
        console.log(this.list)
      })
    },
    changeClass(id) {
      this.classDetail.c_id = this.list[id].id
      this.classDetail.new_name = this.list[id].name
      this.classDetail.new_introduction = this.list[id].introduction
      this.changeShow = true
    },
    enterClass(id) {
      console.log(id)
      this.$router.push({ path: '/teacher/studentManagement', query: { c_id: id }})
    },
    testManage(id) {
      console.log(id)
      this.$router.push({ path: '/teacher/testManage', query: { c_id: id }})
    },
    handleChange() {
      teacherChangeClass(this.classDetail).then(response => {
        if (response.code == 200) {
          this.fetchData()
        } else {
          console.log('出现错误')
        }
      })
      this.changeShow = false
    },
    addClass() {
      this.addShow = true
      console.log(this.$store.getters.id)
    },
    handleAdd() {
      console.log(this.newClass)
      teacherAddClass(this.newClass).then(response => {
        if (response.code == 200) {
          this.fetchData()
        } else {
          console.log('出现问题')
        }
      })
      this.addShow = false
    }
  }
}
</script>

